<?php

/*
	Class: Communitas_Integrate_Model_Customer_Api

	About: Author
		Rich Joslin

	About: License
		<http://communit.as/docs/license>
*/
class Communitas_Integrate_Model_Customer_Api extends Mage_Customer_Model_Customer_Api {

	/* Group: Properties */

	/*
		Property: $_mapAttributes
	*/
	protected $_mapAttributes = array('customer_id' => 'entity_id');

	/* Group: Instance Methods */

	/*
		Function: getCheckoutSession

		Returns: checkout session ID string
	*/
	public function getCheckoutSession() {
		$session = Mage::getModel('checkout/session');
	}

	/*
		Function: create

		Arguments:
			$customerData - TBD

		Returns: customer ID string
	*/
	public function create($customerData) {
		try {
			$customer = Mage::getModel('customer/customer')->setData($customerData)->save();
		} catch (Mage_Core_Exception $e) {
			$this->_fault('data_invalid', $e->getMessage());
		} catch (Exception $e) {
			$this->_fault('data_invalid', $e->getMessage());
		}
		return $customer->getId();
	}

	/*
		Function: info

		Arguments:
			$customerId - TBD

		Returns: customer info array
	*/
	public function info($customerId, $attributes = null) {
		$customer = Mage::getModel('customer/customer')->load($customerId);
		if (!$customer->getId()) {
			$this->_fault('not_exists');
		}
		if (!is_null($attributes) && !is_array($attributes)) {
			$attributes = array($attributes);
		}
		$result = array();
		foreach ($this->_mapAttributes as $attributeAlias=>$attributeCode) {
			$result[$attributeAlias] = $customer->getData($attributeCode);
		}
		foreach ($this->getAllowedAttributes($customer, $attributes) as $attributeCode=>$attribute) {
			$result[$attributeCode] = $customer->getData($attributeCode);
		}
		return $result;
	}

	/*
		Function: items

		Arguments:
			$filters - TBD

		Returns: customer list array
	*/
	public function items($filters) {
		$collection = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*');
		if (is_array($filters)) {
			try {
				foreach ($filters as $field => $value) {
					if (isset($this->_mapAttributes[$field])) {
						$field = $this->_mapAttributes[$field];
					}
					$collection->addFieldToFilter($field, $value);
				}
			} catch (Mage_Core_Exception $e) {
				$this->_fault('filters_invalid', $e->getMessage());
			}
		}
		$result = array();
		foreach ($collection as $customer) {
			$data = $customer->toArray();
			$row  = array();
			foreach ($this->_mapAttributes as $attributeAlias => $attributeCode) {
				$row[$attributeAlias] = (isset($data[$attributeCode]) ? $data[$attributeCode] : null);
			}
			foreach ($this->getAllowedAttributes($customer) as $attributeCode => $attribute) {
				if (isset($data[$attributeCode])) {
					$row[$attributeCode] = $data[$attributeCode];
				}
			}
			$result[] = $row;
		}
		return $result;
	}

	/*
		Function: update

		Arguments:
			$customerId - TBD
			$customerData - TBD

		Returns: boolean
	*/
	public function update($customerId, $customerData) {
		$customer = Mage::getModel('customer/customer')->load($customerId);
		if (!$customer->getId()) {
			$this->_fault('not_exists');
		}
		foreach ($this->getAllowedAttributes($customer, $attributes) as $attributeCode => $attribute) {
			if (isset($customerData[$attributeCode])) {
				$customer->setData($attributeCode, $customerData[$attributeCode]);
			}
		}
		$customer->save();
		return true;
	}

	/*
		Function: setPassword
	*/
	public function setPassword($customer_id, $password) {
		$customer = Mage::getModel('customer/customer')->load($customer_id);
		if (!$customer->getId()) {
			$this->_fault('not_exists');
		}
		$customer->setPassword($password);
		$customer->save();
		return true;
	}

	/*
		Function: delete

		Arguments:
			$customerId - TBD

		Returns: boolean
	*/
	public function delete($customerId) {
		$customer = Mage::getModel('customer/customer')->load($customerId);
		if (!$customer->getId()) {
			$this->_fault('not_exists');
		}
		try {
			$customer->delete();
		} catch (Mage_Core_Exception $e) {
			$this->_fault('not_deleted', $e->getMessage());
		}
		return true;
	}

	/*
		Function: login

		Arguments:
			$user - TBD
			$password - TBD

		Returns: session ID string
	*/
	public function login($user, $password) {
		if (empty($user) || empty($password)) {
			$this->_fault('invalid_input');
		}
		Mage::getSingleton('core/session', array('name' => 'frontend'));
		$session = Mage::getSingleton('customer/session');
		try {
			$session->login($user, $password);
			$session->setCustomerAsLoggedIn($session->getCustomer());
			return $session->getSessionId();
			// return Mage::helper('core')->encrypt($session->getSessionId());
		} catch (Exception $e) {        
			switch ($e->getCode()) {
				case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
					$this->_fault('not_confirmed');
					break;
				case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
					$this->_fault('not_exists');
					break;
				default:
					$this->_fault('unknown_error');
			}
		}
	}

	/*
		Function: logout

		Returns: void
	*/
	public function logout() {
		Mage::getSingleton('customer/session')->logout();
	}

	/*
		Function: subscribe
	*/
	public function subscribe($email) {
		$customer = Mage::getModel('customer/customer')->loadByEmail($email);
		$customer->setIsSubscribed(true);
		return $customer->save();
	}

	/*
		Function: unsubscribe
	*/
	public function unsubscribe($email) {
		$customer = Mage::getModel('customer/customer')->loadByEmail($email);
		$customer->setIsSubscribed(false);
		return $customer->save();
	}

}
